package com.example.demo01.i18n;

import com.example.demo01.util.PropertiesUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Locale;
import java.util.Properties;
import java.util.TimeZone;

/**
 * @author wangxun
 */
@Slf4j
@Component
public class InternationalizeTools {

    @Value("${spring.application.name}")
    private String applicationName;

    @Autowired
    private MessageSource messageSource;


    private String getConfigProperties(String name) {
        return this.messageSource.getMessage(name, (Object[]) null, (Locale) null);
    }

    public String getI18nProperties(String serviceCode, String name) {
        String value = null;
        try {
            Locale locale = LocaleContextHolder.getLocale();
            String fileName = this.getFileName(serviceCode, locale);
            Properties properties = PropertiesUtil.readPropertiesFile("/i18n/" + fileName);
            if (ObjectUtils.isEmpty(properties)) {
                log.error("获取配置异常!异常信息:{}", "没有找到对应的配置文件");
            } else {
                value = properties.getProperty(name);
            }
        } catch (IOException e) {
            log.error("获取本地配置文件异常!异常信息:{}", e.toString());
        }

        return value;
    }

    public String getI18nDBProperties(String serviceCode, String name) {
        String value = null;
        try {
            Locale locale = LocaleContextHolder.getLocale();
            String fileName = this.getDBFileName(serviceCode, locale);
            Properties properties = PropertiesUtil.readPropertiesFile("/i18n/db/" + fileName);
            if (ObjectUtils.isEmpty(properties)) {
                log.error("获取配置异常!异常信息:{}", "没有找到对应的配置文件");
            } else {
                value = properties.getProperty(name);
            }
        } catch (IOException e) {
            log.error("获取本地配置文件异常!异常信息:{}", e.toString());
        }

        return value;
    }

    public String formatI18nProperties(String serviceCode, String name, Object... args) {
        String value = "";
        value = this.getI18nProperties(serviceCode, name);
        return StringUtils.isEmpty(value) ? value : String.format(value, args);
    }

    public String formatI18nDBProperties(String serviceCode, String name, Object... args) {
        String value = "";
        value = this.getI18nDBProperties(serviceCode, name);
        return StringUtils.isEmpty(value) ? value : String.format(value, args);
    }

    public String setMqttLang(String lang) {
        try {
            String[] language = lang.split("_");
            Locale locale = Locale.getDefault();
            locale = new Locale(language[0], language[1]);
            LocaleContextHolder.setLocale(locale);
        } catch (NoSuchMessageException var4) {
            log.error("获取配置异常!异常信息:{}", var4);
        }

        return null;
    }

    public String getPropertiesByLocale(String serviceCode, String name, Locale l) {
        String fileName = "";
        String lang = "";
        String value = "";

        try {
            fileName = this.getFileName(serviceCode, l);
            Properties properties = null;
            properties = PropertiesUtil.readPropertiesFile("/i18n/" + fileName);
            if (ObjectUtils.isEmpty(properties)) {
                log.error("获取配置异常!异常信息:{}", "没有找到对应的配置文件");
            } else {
                value = properties.getProperty(name);
            }
        } catch (IOException var8) {
            log.error("获取本地配置文件异常!异常信息:{}", var8);
        }

        return value;
    }

    public String getDBPropertiesByLocale(String serviceCode, String name, Locale l) {
        String fileName = "";
        String lang = "";
        String value = "";

        try {
            fileName = this.getDBFileName(serviceCode, l);
            Properties properties = null;
            properties = PropertiesUtil.readPropertiesFile("/i18n/db/" + fileName);
            if (ObjectUtils.isEmpty(properties)) {
                log.error("获取配置异常!异常信息:{}", "没有找到对应的配置文件");
            } else {
                value = properties.getProperty(name);
            }
        } catch (IOException var8) {
            log.error("获取本地配置文件异常!异常信息:{}", var8);
        }

        return value;
    }

    public String formatPropertiesByLocale(String serviceCode, String name, Locale l, Object... args) {
        String value = "";
        value = this.getPropertiesByLocale(serviceCode, name, l);
        return StringUtils.isEmpty(value) ? value : String.format(value, args);
    }

    public String formatDBPropertiesByLocale(String serviceCode, String name, Locale l, Object... args) {
        String value = "";
        value = this.getDBPropertiesByLocale(serviceCode, name, l);
        return StringUtils.isEmpty(value) ? value : String.format(value, args);
    }

    public String getProperties(String name) {
        try {
            Locale locale = LocaleContextHolder.getLocale();
            if (ObjectUtils.isNotEmpty(locale)) {
                log.info("加载本地的语言信息------------>【{}_{}】", locale.getLanguage(), locale.getCountry());
            }

            return this.messageSource.getMessage(name, (Object[]) null, locale);
        } catch (NoSuchMessageException var3) {
            log.error("获取配置异常!异常信息:{}", var3);
            return null;
        }
    }

    private String getFileName(String serviceCode, Locale locale) {
        if (ObjectUtils.isNotEmpty(locale)) {
            String lang = "_" + locale.getLanguage() + "_" + locale.getCountry();
            log.info("加载本地的语言信息------------>【{}_{}】", locale.getLanguage(), locale.getCountry());
            return this.applicationName + "-" + serviceCode + lang + "-message.properties";
        } else {
            return "";
        }
    }

    private String getDBFileName(String serviceCode, Locale locale) {
        if (ObjectUtils.isNotEmpty(locale)) {
            String lang = "_" + locale.getLanguage() + "_" + locale.getCountry();
            log.info("加载本地的语言信息------------>【{}_{}】", locale.getLanguage(), locale.getCountry());
            return this.applicationName + "-" + serviceCode + lang + "-db-message.properties";
        } else {
            return "";
        }
    }

    public String setMqttTimeZoneId(String timeZoneId) {
        try {
            TimeZone timeZone = TimeZone.getTimeZone(timeZoneId);
            LocaleContextHolder.setTimeZone(timeZone);
        } catch (NoSuchMessageException var3) {
            log.error("获取时区异常!异常信息:{}", var3);
        }

        return null;
    }

    public String getMqttTimeZoneId() {
        TimeZone timeZone = LocaleContextHolder.getTimeZone();
        return timeZone.getID();
    }

    public String getMqttProperties(String name, String lang) {
        try {
            Locale locale = new Locale(lang);
            if (ObjectUtils.isNotEmpty(locale)) {
                log.info("mqtt本地的语言信息------------>【{}_{}】", locale.getLanguage(), locale.getCountry());
            }

            return this.messageSource.getMessage(name, (Object[]) null, locale);
        } catch (NoSuchMessageException var4) {
            log.error("获取配置异常!异常信息:{}", var4);
            return null;
        }
    }

    public String formatProperties(String name, Object... args) {
        try {
            Locale locale = LocaleContextHolder.getLocale();
            String val = "";
            val = this.messageSource.getMessage(name, (Object[]) null, locale);
            return StringUtils.isEmpty(val) ? val : String.format(val, args);
        } catch (NoSuchMessageException var5) {
            log.error("获取配置异常!异常信息:{}", var5);
            return null;
        }
    }

    public String getPropertiesByLocale(String name, Locale locale) {
        try {
            if (ObjectUtils.isNotEmpty(locale)) {
                log.info("加载本地的语言信息------------>【{}_{}】", locale.getLanguage(), locale.getCountry());
                return this.messageSource.getMessage(name, (Object[]) null, locale);
            }
        } catch (NoSuchMessageException var4) {
            log.error("获取配置异常!异常信息:{}", var4);
        }

        return null;
    }

    public String formatPropertiesByLocale(String name, Locale locale, Object... args) {
        try {
            if (ObjectUtils.isNotEmpty(locale)) {
                String val = "";
                val = this.messageSource.getMessage(name, (Object[]) null, locale);
                if (StringUtils.isEmpty(val)) {
                    return val;
                }

                return String.format(val, args);
            }
        } catch (NoSuchMessageException var5) {
            log.error("获取配置异常!异常信息:{}", var5);
        }

        return null;
    }
}
